package com.support.ohos.designlibdemo;

import com.support.ohos.designlibdemo.pref.DisplayMode;
import com.support.ohos.designlibdemo.utils.Cheeses;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.window.service.WindowManager;

public class DetailAbility extends Ability implements Component.ClickedListener {
    private Component mCptStatusHolder;
    private Component mDlTitleLayout;
    private Component mImgTitleMenu;
    private Component mDlTitleSet;


    private Component mCptPopupHolder;
    private Component mDlPopupRoot;
    private Component mDlPopupItem;
    private Component mDlPopupItemDay;
    private Component mDlPopupItemNight;


    private Component mDlScrollRoot;
    private Component mDlScrollItems;
    private Component mDlScrollTop;
    private Component mDlScrollItemCardInfo;
    private Component mDlScrollItemCardFriend;
    private Component mDlScrollItemCardRelated;
    private Text mTxtScrollItemInfoName;
    private Text mTxtScrollItemInfoDesc;
    private Text mTxtScrollItemFriendName;
    private Text mTxtScrollItemFriendDesc;
    private Text mTxtScrollItemRelatedName;
    private Text mTxtScrollItemRelatedDesc;
    private Text mTxtTopName;
    private Text mTxtScrollName;
    private Component mDlFloatActionMsg;

    private Element mScrollTopElement;

    private AnimatorProperty mStatusAnimator;
    private AnimatorProperty mImgFlatAnimator;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_detail);
        initViewPage();
        initViewListener();
        process();
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        if (mStatusAnimator != null) {
            mStatusAnimator = null;
        }
        if (mImgFlatAnimator != null) {
            mImgFlatAnimator = null;
        }
    }

    @Override
    public void onClick(Component cpt) {
        switch (cpt.getId()) {
            case ResourceTable.Id_img_detail_title_menu:
                this.terminateAbility();
                break;
            case ResourceTable.Id_dl_detail_title_set:
                showPopupRoot();
                break;
            case ResourceTable.Id_cpt_common_popup_holder:
                disPopupRoot();
                disPopupItem();
                break;
            case ResourceTable.Id_dl_common_popup_root:
                showPopupItem();
                break;
            case ResourceTable.Id_dl_common_popup_item_day:
            case ResourceTable.Id_dl_common_popup_item_night:
                disPopupItem();
                break;
        }
    }

    /**
     * 初始化页面组件View
     */
    private void initViewPage() {
        mCptStatusHolder = findComponentById(ResourceTable.Id_cpt_detail_status_holder);
        mDlTitleLayout = findComponentById(ResourceTable.Id_dl_detail_title);
        mImgTitleMenu = findComponentById(ResourceTable.Id_img_detail_title_menu);
        mDlTitleSet = findComponentById(ResourceTable.Id_dl_detail_title_set);

        mCptPopupHolder = findComponentById(ResourceTable.Id_cpt_common_popup_holder);
        mDlPopupRoot = findComponentById(ResourceTable.Id_dl_common_popup_root);
        mDlPopupItem = findComponentById(ResourceTable.Id_dl_common_popup_item);
        mDlPopupItemDay = findComponentById(ResourceTable.Id_dl_common_popup_item_day);
        mDlPopupItemNight = findComponentById(ResourceTable.Id_dl_common_popup_item_night);

        mDlScrollRoot = findComponentById(ResourceTable.Id_dl_detail_scroll);
        mDlScrollItems = findComponentById(ResourceTable.Id_dl_detail_scroll_items);
        mDlScrollTop = findComponentById(ResourceTable.Id_dl_detail_scroll_top);
        mDlScrollItemCardInfo = findComponentById(ResourceTable.Id_dl_detail_scroll_item_card_info);
        mDlScrollItemCardFriend =
                findComponentById(ResourceTable.Id_dl_detail_scroll_item_card_friend);
        mDlScrollItemCardRelated =
                findComponentById(ResourceTable.Id_dl_detail_scroll_item_card_related);
        mDlFloatActionMsg = findComponentById(ResourceTable.Id_dl_detail_msg_flat);
        mScrollTopElement = new PixelMapElement(Cheeses.getResourceFromMedia(this, Cheeses.randomCheeseDrawable()));
        initViewPage1();
    }

    private void initViewPage1() {
        Component txtScrollItemInfoName = findComponentById(ResourceTable.Id_txt_detail_scroll_item_info_name);
        if (txtScrollItemInfoName instanceof Text) {
            mTxtScrollItemInfoName = (Text) txtScrollItemInfoName;
        }
        Component txtScrollItemInfoDesc = findComponentById(ResourceTable.Id_txt_detail_scroll_item_info_desc);
        if (txtScrollItemInfoDesc instanceof Text) {
            mTxtScrollItemInfoDesc = (Text) txtScrollItemInfoDesc;
        }
        Component txtScrollItemFriendName = findComponentById(ResourceTable.Id_txt_detail_scroll_item_friend_name);
        if (txtScrollItemFriendName instanceof Text) {
            mTxtScrollItemFriendName = (Text) txtScrollItemFriendName;
        }
        Component txtScrollItemFriendDesc = findComponentById(ResourceTable.Id_txt_detail_scroll_item_friend_desc);
        if (txtScrollItemFriendDesc instanceof Text) {
            mTxtScrollItemFriendDesc = (Text) txtScrollItemFriendDesc;
        }
        Component txtScrollItemRelatedName = findComponentById(ResourceTable.Id_txt_detail_scroll_item_related_name);
        if (txtScrollItemRelatedName instanceof Text) {
            mTxtScrollItemRelatedName = (Text) txtScrollItemRelatedName;
        }
        Component txtScrollItemRelatedDesc = findComponentById(ResourceTable.Id_txt_detail_scroll_item_related_desc);
        if (txtScrollItemRelatedDesc instanceof Text) {
            mTxtScrollItemRelatedDesc = (Text) txtScrollItemRelatedDesc;
        }
        Component txtTopName = findComponentById(ResourceTable.Id_txt_detail_top_name);
        if (txtTopName instanceof Text) {
            mTxtTopName = (Text) txtTopName;
        }
        Component txtScrollName = findComponentById(ResourceTable.Id_txt_detail_scroll_name);
        if (txtScrollName instanceof Text) {
            mTxtScrollName = (Text) txtScrollName;
        }
    }

    /**
     * 初始化页面监听器Listener
     */
    private void initViewListener() {
        mDlScrollRoot.setScrolledListener(scrolledListener);
        mImgTitleMenu.setClickedListener(this);
        mDlTitleSet.setClickedListener(this);

        mCptPopupHolder.setClickedListener(this);
        mDlPopupRoot.setClickedListener(this);
        mDlPopupItemDay.setClickedListener(this);
        mDlPopupItemNight.setClickedListener(this);
    }

    /**
     * 业务处理
     */
    private void process() {
        initStatusBar();
        setScrollTopPixelMap();
        initFloatActionMsg();
        applyDisplayMode();
        getCheeseName();
        mCptStatusHolder.setAlpha(0);
        mTxtTopName.setAlpha(0);
        mScrollNameSize = mTxtScrollName.getTextSize();
    }

    private boolean mCurTopState;
    private boolean mCurNameState;
    private int mScrollNameSize = 0;
    private float mScrollTopHeight = 0;

    private final Component.ScrolledListener scrolledListener =
            (component, scrollX, scrollY, oldScrollX, oldScrollY) -> {
                int scrollTopHeight = mDlScrollTop.getHeight();
                if (mScrollTopHeight < scrollTopHeight) {
                    mScrollTopHeight = scrollTopHeight;
                }
                float scale = scrollTopHeight / mScrollTopHeight;
                mTxtScrollName.setTextSize((int) (mScrollNameSize * scale));
                boolean topState;
                boolean nameState;
                topState = scrollY > mDlScrollItems.getContentPositionY() - 300;
                nameState = scrollY > mTxtScrollName.getContentPositionY() + 280;
                if (mCurTopState != topState) {
                    transparentTop(topState);
                }
                if (mCurNameState != nameState) {
                    Cheeses.setBgColor(this, mDlTitleLayout, nameState ?
                            ResourceTable.Color_colorPrimary :
                            ResourceTable.Color_transparent);
                    mTxtTopName.setAlpha(nameState ? 1 : 0);
                }
                mCurTopState = topState;
                mCurNameState = nameState;
            };

    private final Animator.StateChangedListener stateChangedListener = new Animator.StateChangedListener() {
        @Override
        public void onStart(Animator animator) {
        }

        @Override
        public void onStop(Animator animator) {
        }

        @Override
        public void onCancel(Animator animator) {
        }

        @Override
        public void onEnd(Animator animator) {
            mCptStatusHolder.setAlpha(mCurTopState ? 1 : 0);
        }

        @Override
        public void onPause(Animator animator) {
        }

        @Override
        public void onResume(Animator animator) {
        }
    };

    /**
     * 初始化状态栏
     */
    private void initStatusBar() {
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        int sh = DisplayMode.getScreenHeight(this);
        int dh = DisplayMode.getDisplayHeight(this);
        int height = sh - dh;
        ComponentContainer.LayoutConfig temp = mCptStatusHolder.getLayoutConfig();
        if (temp instanceof DirectionalLayout.LayoutConfig) {
            DirectionalLayout.LayoutConfig lc = (DirectionalLayout.LayoutConfig) temp;
            lc.height = height / 2;
            mCptStatusHolder.setLayoutConfig(lc);
        }
    }

    private void initFloatActionMsg() {
        Cheeses.initFloatAction(this, mDlFloatActionMsg);
    }

    /**
     * 应用显示模式
     */
    private void applyDisplayMode() {
        changeBgOfStatusBar();
        changeBgOfScroll();
    }

    /**
     * 获取CheeseName
     */
    private void getCheeseName() {
        Intent intent = getIntent();
        String name = null;
        if (intent != null) {
            if (intent.hasParameter(Cheeses.EXTRA_NAME)) {
                name = intent.getStringParam(Cheeses.EXTRA_NAME);
            }
        }
        if (name != null) {
            mTxtTopName.setText(name);
            mTxtScrollName.setText(name);
        }
    }

    /**
     * 切换状态栏背景
     */
    private void changeBgOfStatusBar() {
        boolean night = DisplayMode.isNight();
        Cheeses.setBgColor(this, mCptStatusHolder, night ?
                ResourceTable.Color_statusBgNight :
                ResourceTable.Color_statusBgDay);
    }

    /**
     * 切换Scroll背景
     */
    private void changeBgOfScroll() {
        boolean night = DisplayMode.isNight();
        Cheeses.setBgColor(this, mDlScrollRoot, night ?
                ResourceTable.Color_detail_bg_night :
                ResourceTable.Color_white);
        scrollBg(night);
        scrollText(night);
        scrollDesc(night);
    }

    private void scrollBg(boolean night) {
        mDlScrollItemCardInfo.setBackground(
                Cheeses.getGraphicElement(this, night ?
                        ResourceTable.Graphic_bg_detail_item_card_night :
                        ResourceTable.Graphic_bg_detail_item_card_day));
        mDlScrollItemCardFriend.setBackground(
                Cheeses.getGraphicElement(this, night ?
                        ResourceTable.Graphic_bg_detail_item_card_night :
                        ResourceTable.Graphic_bg_detail_item_card_day));
        mDlScrollItemCardRelated.setBackground(
                Cheeses.getGraphicElement(this, night ?
                        ResourceTable.Graphic_bg_detail_item_card_night :
                        ResourceTable.Graphic_bg_detail_item_card_day));
    }

    private void scrollText(boolean night) {
        mTxtScrollItemInfoName.setTextColor(
                Cheeses.getColorFromRes(this, night ?
                        ResourceTable.Color_detail_text_name_night :
                        ResourceTable.Color_detail_text_name_day));
        mTxtScrollItemFriendName.setTextColor(
                Cheeses.getColorFromRes(this, night ?
                        ResourceTable.Color_detail_text_name_night :
                        ResourceTable.Color_detail_text_name_day));
        mTxtScrollItemRelatedName.setTextColor(
                Cheeses.getColorFromRes(this, night ?
                        ResourceTable.Color_detail_text_name_night :
                        ResourceTable.Color_detail_text_name_day));
    }

    private void scrollDesc(boolean night) {
        mTxtScrollItemInfoDesc.setTextColor(
                Cheeses.getColorFromRes(this, night ?
                        ResourceTable.Color_detail_text_desc_night :
                        ResourceTable.Color_detail_text_desc_day));
        mTxtScrollItemFriendDesc.setTextColor(
                Cheeses.getColorFromRes(this, night ?
                        ResourceTable.Color_detail_text_desc_night :
                        ResourceTable.Color_detail_text_desc_day));
        mTxtScrollItemRelatedDesc.setTextColor(
                Cheeses.getColorFromRes(this, night ?
                        ResourceTable.Color_detail_text_desc_night :
                        ResourceTable.Color_detail_text_desc_day));
    }

    private void setScrollTopPixelMap() {
        if (mScrollTopElement == null) {
            mScrollTopElement = new PixelMapElement(Cheeses.getResourceFromMedia(this, Cheeses.randomCheeseDrawable()));
        }
        mDlScrollTop.setBackground(mScrollTopElement);
    }

    private void transparentTop(boolean state) {
        if (state) {
            ShapeElement shape;
            shape = new ShapeElement();
            shape.setRgbColor(RgbColor.fromArgbInt(
                    Cheeses.getColorVersion4(this, ResourceTable.Color_colorPrimary)));
            mDlScrollTop.setBackground(shape);
        } else {
            if (mScrollTopElement != null) {
                mDlScrollTop.setBackground(mScrollTopElement);
            } else {
                mDlScrollTop.setBackground(new PixelMapElement(
                        Cheeses.getResourceFromMedia(this, Cheeses.randomCheeseDrawable())));
            }
        }

        if (mStatusAnimator == null) {
            mStatusAnimator = mCptStatusHolder.createAnimatorProperty();
            mStatusAnimator.setStateChangedListener(stateChangedListener);
        }
        if (mImgFlatAnimator == null) {
            mImgFlatAnimator = mDlFloatActionMsg.createAnimatorProperty();
        }
        float scaleFrom = state ? 1 : 0;
        float scale = state ? 0 : 1;
        mStatusAnimator.alpha(state ? 1 : 0).setDuration(100);
        mImgFlatAnimator.scaleXBy(scaleFrom).scaleYBy(scaleFrom).scaleX(scale).scaleY(scale).setDuration(100);
        mStatusAnimator.start();
        mImgFlatAnimator.start();
    }

    /**
     * 显示PopupRoot
     */
    private void showPopupRoot() {
        if (mDlPopupRoot.getVisibility() != Component.VISIBLE) {
            mDlPopupRoot.setVisibility(Component.VISIBLE);
        }
        showPopupHolder();
    }

    /**
     * 关闭PopupRoot
     */
    private void disPopupRoot() {
        if (mDlPopupRoot.getVisibility() != Component.HIDE) {
            mDlPopupRoot.setVisibility(Component.HIDE);
        }
        disPopupHolder();
    }

    /**
     * 显示PopupItem
     */
    private void showPopupItem() {
        if (mDlPopupItem.getVisibility() != Component.VISIBLE) {
            mDlPopupItem.setVisibility(Component.VISIBLE);
        }
        disPopupRoot();
        showPopupHolder();
    }

    /**
     * 关闭PopupItem
     */
    private void disPopupItem() {
        if (mDlPopupItem.getVisibility() != Component.HIDE) {
            mDlPopupItem.setVisibility(Component.HIDE);
        }
        disPopupHolder();
    }

    /**
     * 显示PopupHolder
     */
    private void showPopupHolder() {
        if (mCptPopupHolder.getVisibility() != Component.VISIBLE) {
            mCptPopupHolder.setVisibility(Component.VISIBLE);
        }
    }

    /**
     * 关闭PopupHolder
     */
    private void disPopupHolder() {
        if (mCptPopupHolder.getVisibility() != Component.HIDE) {
            mCptPopupHolder.setVisibility(Component.HIDE);
        }
    }

}